﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PlatForm.DataLayer;

namespace PlatForm.Controller
{
    public class KPIController
    {
        public List<Kpi> GetAll(DateTime d)
        {
            var lstKpi = new List<Kpi>();
            var lstPick = Picking.Objects.Where(x => x.PickDate == d.Date).ToList();
            var lstPickNo = lstPick.Select(x => x.PIK_No);

            var lstPickDetail = PickingDetail.Objects.Where(x => lstPickNo.Contains(x.PIK_No)).ToList();

            var lstOrderNo = lstPick.Select(x => x.ORD_No).ToList();

            var lstOrder = Order.Objects.Where(x => lstOrderNo.Contains(x.ORD_No)).ToList();
            var lstArticleNo = lstOrder.Select(x => x.ART_No).ToList();

            var lstArticle = Article.Objects.Where(x => lstArticleNo.Contains(x.ART_No)).ToList();
            var lstSupplierNo = lstPick.Select(x => x.SUPPL_No).ToList();

            var lstSupplier = Supplier.Objects.Where(x => lstSupplierNo.Contains(x.SUPPL_No)).ToList();
            
            foreach (var p in lstPick)
            {
                float unit = 0;
                var kpi = new Kpi();
                kpi.Date = d;
                kpi.SUPPL_No = p.SUPPL_No;
                kpi.ORD_No = p.ORD_No;
                kpi.ART_No=p.ART_No;
                kpi.ST_No = p.ST_No;
                kpi.DELI_QTY = p.DELI_QTY;

                var f = lstSupplier.FirstOrDefault(x => x.SUPPL_No == kpi.SUPPL_No);
                if (f != null)
                    kpi.SUPPL_Name = f.SUPPL_Name;
                var a = lstArticle.FirstOrDefault(x => x.ART_No == kpi.ART_No);
                if (a != null)
                {
                    kpi.ART_Name = a.ART_Name;
                    kpi.Cat = a.Cat;
                    unit = a.Unit;
                }
                var o = lstOrder.FirstOrDefault(x => x.ORD_No == kpi.ORD_No);
                if (o != null)
                    kpi.ORD_QTY = o.DELI_QTY;

                kpi.ORD_QTY_KG = kpi.ORD_QTY*unit;
                kpi.DELI_QTY_KG = kpi.DELI_QTY * unit;
           
                if (kpi.Cat == 1)
                {
                    kpi.TeU = kpi.DELI_QTY_KG/1000;
                    kpi.Cat1 = kpi.DELI_QTY_KG;
                }
                else
                {
                    kpi.TeU = kpi.DELI_QTY_KG * kpi.Cat;
                    switch (kpi.Cat)
                    {
                        case 2:
                            kpi.Cat2 = kpi.DELI_QTY_KG;
                            break;
                        case 3:
                            kpi.Cat3 = kpi.DELI_QTY_KG;
                            break;
                        case 4:
                            kpi.Cat4 = kpi.DELI_QTY_KG;
                            break;
                    }
                }


                kpi.NaturalLoss = lstPickDetail.Sum(x => x.NaturalLoss);
                kpi.ProcessLoss = lstPickDetail.Sum(x => x.ProcessLoss);

                lstKpi.Add(kpi);
            }
            
            return lstKpi;
        }
    }
}
